package com.dateMasking;

import com.alibaba.excel.metadata.CellData;
import com.alibaba.excel.metadata.Head;
import com.alibaba.excel.write.handler.CellWriteHandler;
import com.alibaba.excel.write.metadata.holder.WriteSheetHolder;
import com.alibaba.excel.write.metadata.holder.WriteTableHolder;
import org.apache.commons.lang3.ArrayUtils;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;

import java.util.List;

public class CustomCellWriteHandler implements CellWriteHandler {



    /**
     * 需要进行脱敏的列
     * */
    private Integer[] indexs;

    public CustomCellWriteHandler(Integer[] indexs){
        this.indexs = indexs;
    }

    @Override
    public void beforeCellCreate(WriteSheetHolder writeSheetHolder, WriteTableHolder writeTableHolder, Row row, Head head, Integer columnIndex, Integer relativeRowIndex, Boolean isHead) {

    }

    @Override
    public void afterCellCreate(WriteSheetHolder writeSheetHolder, WriteTableHolder writeTableHolder, Cell cell, Head head, Integer relativeRowIndex, Boolean isHead) {
//        if(!isHead){
//            System.err.println(cell.getStringCellValue());
//        }
    }

    @Override
    public void afterCellDataConverted(WriteSheetHolder writeSheetHolder, WriteTableHolder writeTableHolder, CellData cellData, Cell cell, Head head, Integer relativeRowIndex, Boolean isHead) {
//        if(!isHead){
//            if(this.masking()){
//                int row = cell.getRowIndex();//行
//                int column = cell.getColumnIndex();//列
//                //System.err.println(ArrayUtils.contains(indexs,column));
//                if(ArrayUtils.contains(indexs,column)){
//                    String value = cellData.getStringValue();
//                    value = value.replaceAll(".*","*");
//                    System.err.println(value);
//                    cellData.setData("value");
//                }
//            }
//
//        }
    }

    @Override
    public void afterCellDispose(WriteSheetHolder writeSheetHolder, WriteTableHolder writeTableHolder, List<CellData> cellDataList, Cell cell, Head head, Integer relativeRowIndex, Boolean isHead) {

        if(!isHead){
            for(int i= 0;i< indexs.length;i++){
                CellData cellData = cellDataList.get(indexs[i]);
                System.err.println(cellData.getStringValue());
                cellData.setStringValue("*");
            }
        }
    }

    /**
     * 是否要脱敏处理
     * */
    private Boolean masking(){
        return true;
    }

}
